/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LogIn1.java
 *
 * Created on Sep 24, 2011, 12:40:38 PM
 */
package inventory;

/**
 *
 * @author Mark King
 */

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class LogInUI extends javax.swing.JPanel{

    
    public String name;
    public String password;
    private Connection conn;
    private UserQuery user;
    private boolean verify = false;
    
    private int width= 533;
    private int height = 326;
    
    private int desktopScreenHeight;
    private int desktopScreenWidth;

    private MainUI mainMenu;
    private JFrame mainMenuFrame;
    /** Creates new form LogIn1 */
    public LogInUI(Connection conn, int screenWidth, int screenHeight) {
        this.conn = conn;
        this.desktopScreenWidth = screenWidth;
        this.desktopScreenHeight = screenHeight;
        user = new UserQuery(conn);
        initComponents();
    }

    
    public int getWidth()
    {
        return width;
    }
    
    public int getHeight()
    {
        return height;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userNameTextBox = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        passwordTextBox = new javax.swing.JPasswordField();

        userNameTextBox.setFont(new java.awt.Font("Tahoma", 1, 18));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Password:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("User Name:");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jButton1.setText("Log In");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Log In");

        passwordTextBox.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        passwordTextBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordTextBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passwordTextBox, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                            .addComponent(userNameTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(166, 166, 166)
                        .addComponent(jButton1)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userNameTextBox, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                    .addComponent(jLabel2))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jButton1)
                .addGap(27, 27, 27))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try {
            if(user.checkUser(userNameTextBox.getText(), passwordTextBox.getText())){
            JOptionPane.showMessageDialog(null, "Successfully Logged in as " +  user.getName());
            mainMenuFrame = new JFrame("Global Air Technology Inventory");
            mainMenu = new MainUI(conn, user.getName(), desktopScreenWidth, desktopScreenHeight);
            mainMenuFrame.add(mainMenu);
            mainMenuFrame.setSize(mainMenu.getWidth(), mainMenu.getHeight());
            mainMenuFrame.setLocation((desktopScreenWidth -mainMenu.getWidth()) /2, (desktopScreenHeight-mainMenu.getHeight())/2);
            mainMenuFrame.setResizable(false);
            mainMenuFrame.setVisible(true);
            mainMenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            verify = true;
            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    try{
                        if(mainMenu.closeWindows){
                            mainMenuFrame.dispose();
                            mainMenu.customer.closeWindows=false;
                            mainMenu.supplier.closeWindows=false;
                            mainMenu.inventory.closeWindows=false;
                            mainMenu.install.closeWindows=false;
                            mainMenu.order.closeWindows=false;
                            try{
                                mainMenu.order.customer.closeWindows=false;
                                mainMenu.order.company.closeWindows=false;
                            }catch(Exception e){}
                            mainMenu.account.closeWindows=false;
                            mainMenu.closeWindows=false;
                            Driver d=new Driver();
                        }
                    }catch(Exception e){}
                }
            };
            Timer timer = new Timer(500, actionListener);
            timer.start();
            }
            else
            JOptionPane.showMessageDialog(null, "         Access Denied!");
            
        } catch (SQLException ex) {
            Logger.getLogger(LogInUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(LogInUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
}//GEN-LAST:event_jButton1ActionPerformed

private void passwordTextBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordTextBoxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_passwordTextBoxActionPerformed

public boolean getVerify()
{
    return this.verify;
}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField passwordTextBox;
    private javax.swing.JTextField userNameTextBox;
    // End of variables declaration//GEN-END:variables
}
